﻿using System;
using System.Data;
using System.Reflection;
using Token.Piker.Core;

namespace Token.Piker.Web
{
    public partial class News_Album : PikerBasePage
    {
        public Token.Piker.Model.NewsAlbum AlbumModel = new Token.Piker.Model.NewsAlbum();
        public Token.Piker.Model.NewsAlbum preAlbum = new Token.Piker.Model.NewsAlbum();
        public Token.Piker.Model.NewsAlbum nextAlbum = new Token.Piker.Model.NewsAlbum();
        public string preSRC = "";
        public string nextSRC = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            string NewsAlbID = Request.QueryString["id"];
            if (!string.IsNullOrEmpty(NewsAlbID))
            {
                BindAlbumData(int.Parse(NewsAlbID));
            }
        }

        protected void BindAlbumData(int ID)
        {
            //绑定当前相册信息
            Token.Piker.BLL.NewsAlbumBLL Albumbll = new Token.Piker.BLL.NewsAlbumBLL();
            AlbumModel = Albumbll.GetModel(ID);

            //绑定上一个时间段的相册信息
            //DataSet predata = Albumbll.GetListByPage(" CreateTime < '" + AlbumModel.CreateTime + "' and Flag=0 ", " CreateTime desc ", 1, 1);
            DataSet predata = Albumbll.GetListByPage(" ID < " + AlbumModel.ID + " and Flag=0 ", "ID desc", 1, 1);
            if (predata.Tables[0].Rows.Count > 0)
            {
                preAlbum = (Token.Piker.Model.NewsAlbum)InstantiationBean(preAlbum, predata.Tables[0], 0);
                preSRC = "News_Album.aspx?id=" + preAlbum.ID;
            }
            else
            {
                preAlbum.AlbumName = "(ˇˍˇ） 没了～";
                preSRC = "";
            }

            //绑定下一个时间段的相册信息
            //DataSet nextdata = Albumbll.GetListByPage(" CreateTime > '" + AlbumModel.CreateTime + "' and Flag=0 ", " CreateTime asc ", 1, 1);
            DataSet nextdata = Albumbll.GetListByPage(" ID > " + AlbumModel.ID + " and Flag=0 ", "ID asc", 1, 1);
            if (nextdata.Tables[0].Rows.Count > 0)
            {
                nextAlbum = (Token.Piker.Model.NewsAlbum)InstantiationBean(nextAlbum, nextdata.Tables[0], 0);
                nextSRC = "News_Album.aspx?id=" + nextAlbum.ID;
            }
            else
            {
                nextAlbum.AlbumName = "点击我新建相册去^_^";
                nextSRC = "";
            }

            //获取相册所有图片
            Token.Piker.BLL.NewsPhotoBLL photoBLL = new Token.Piker.BLL.NewsPhotoBLL();
            DataSet allPhotos = photoBLL.GetList("Album=" + ID + "and Flag = 0");
            Photo_rpt.DataSource = allPhotos;
            Photo_rpt.DataBind();
        }

        /// <summary> 
        /// 依据反射机制实例化一个对象  
        /// </summary>   
        /// <param name="obj">要实例的对象  
        /// <param name="dataTable">值[DataTable对象]  
        /// <param name="row">要读取DataTable行的序号  
        /// <returns></returns>返回被实例的对象  
        private Object InstantiationBean(Object obj, DataTable dataTable, int row)
        {
            DataColumnCollection dtcolColl = dataTable.Columns;         //数据结果集列总数  
            DataRowCollection dtrowColl = dataTable.Rows;               //数据结果集行总数  

            //如果数据为空  
            if (dtrowColl.Count == 0)
            {
                return null;
            }

            DataRow dataRow = dtrowColl[row];                             //取出第row行数据  
            Type type = obj.GetType();                                  //建立反射  

            try
            {
                int count = dtcolColl.Count;
                for (int i = 0; i < count; i++)                         //循环数据结果集列名  
                {
                    String colName = dtcolColl[i].ColumnName;           //取出数据结果集列名  
                    PropertyInfo pInfo = type.GetProperty(colName);     //反射机制,动态检索类属性  

                    if (pInfo != null)                                  //如果在类中找到对应的属性值,则将其实例到对象中  
                    {
                        Object objColName = dataRow[colName];
                        if (!Convert.IsDBNull(objColName) && objColName.ToString() != null)    //如果数据集中此列有值  
                        {
                            pInfo.SetValue(obj, objColName, null);
                        }
                        else
                        {
                            pInfo.SetValue(obj, null, null);                   //如果数据集中此列没有值  
                        }
                    }
                }
            }
            catch (Exception ex)
            {                                                    //不捕获异常,异常抛出由开发人员处理  
                throw ex;
            }

            return obj;
        }

    }

}